/**
 *  Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.mapper.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.rshang.rsdp.entity.TbUser;

/**
 * 功能简介：<br/>
 * @author zhangjun
 * @create 2017年8月2日下午3:53:33
 * 
 */
public class SessionHandlerInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if (request.getHeader("x-requested-with") != null&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))//如果是ajax请求响应头会有，x-requested-with；  
		{  
			Subject currentUser = SecurityUtils.getSubject();
			if(!currentUser.isAuthenticated()){
				throw new UnauthorizedException("用户登陆超时，请重新登陆!");
				/*response.setHeader("sessionstatus", "timeout");//在响应头设置session状态      
				response.getWriter().print("timeout");*/
			}
		}//打印一个返回值，没这一行，在tabs页中无法跳出
		return true;
	}

}
